<template>
  <transition name="slide-down">
    <div v-if="isVisible && result.length > 0" class="screen-saver" overflow-hidden z-999 w-screen h-screen @click="closeScreenSaver">
      <nut-swiper
        :init-page="initPage"
        :key="swiperKey"
        w-screen
        h-screen
        :pagination-visible="false"
        :loop="true"
        ref="swiper"
        :is-prevent-default="false"
        :is-stop-propagation="false"
        :auto-play="5000">
        <nut-swiper-item v-for="(item, index) in result" :key="item">
          <img w-screen h-screen object-cover :src="item" />
        </nut-swiper-item>
      </nut-swiper>
    </div>
  </transition>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useConfigStore } from '@/store/modules/config'
const configStore = useConfigStore()
const curTopic = computed(() => configStore.topics)
const initPage = ref(0)
const swiperKey = ref(new Date())
const emit = defineEmits(['close'])
const result = computed(() => 
  (curTopic.value.waitUrl || '').split(',').filter(url => url.trim() !== '')
)
const props = defineProps({
  visible: Boolean
})

const isVisible = ref(props.visible)
watch(
  () => props.visible,
  (newValue) => {
    isVisible.value = newValue
  }
)
// 监听result变化
watch(
  () => result.value,
  (newValue) => {
    initPage.value = 0
    swiperKey.value = new Date()
  }
)

const swiper = ref(null)

const closeScreenSaver = () => {
  console.log('closeScreenSaver')
  emit('close')
}

</script>

<style scoped>
.screen-saver {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.slide-down-enter-active,
.slide-down-leave-active {
  transition: transform 0.3s ease-in-out;
}

.slide-down-enter-from,
.slide-down-leave-to {
  transform: translateY(-100%);
}

.slide-down-enter-to,
.slide-down-leave-from {
  transform: translateY(0);
}
</style>
